<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
/**
 * 申请充值后台控制器
 * @author 
 */
class RechargeController extends AdminController {
    /**
     * 充值记录
     */
    public function index(){
    	//搜索
    	$keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map_user['id|username|email|mobile'] = array($condition, $condition, $condition, $condition,'_multi'=>true);
        $userids=M('User')->where($map_user)->getField('id',true);
        if(!empty($userids)){
        	$map['userid']=array('in',$userids);
        }else{
        	$map['userid']=0;
        }
        
    	//设置Tab导航数据列表
    	//获取可申请充值币种
    	
    	//设置下拉搜索项
    	//根据数据词典表获取申请充值状态
    	$recharge_status_list=M('Descriptions')->where(array('tablename'=>'recharge_status'))->select();
    	$keyselect=I('keyselect');
    	if(!empty($keyselect)){
    		$map['status']=$keyselect;
    	}
    	
    	//预定义按钮属性以简化使用
    	$attr_bz['title'] = '查看备注';
    	$attr_bz['class'] = 'label label-xq';
    	$attr_bz['href'] ='javascript:void(0);';
    	$attr_bz['onclick']='show_recharge_bz(__data_id__)';
    	
    	//iframe层
    	$extra_html='<script type="text/javascript" src="./Public/extra/js/recharge_bz.js" charset="utf-8"></script>';
    	$extra_html.='<script type="text/javascript" src="./Public/extra/js/recharge_review.js" charset="utf-8"></script>';
    	 
    	
    	$attr['field']='status';
    	$attr[1]['title'] = '批准';
    	$attr[1]['class'] = 'label label-success ajax-get confirm';
    	$attr[1]['href']  = U('recharge/review', array('id' => '__data_id__','review_type'=>1));
    	
    	//预定义按钮属性以简化使用
    	$attr1['title'] = '拒绝';
    	$attr1['class'] = 'label label-warning';
    	$attr1['href'] ='javascript:void(0);';
    	$attr1['onclick']='recharge_review_refuse(__data_id__)';
    	
    	$data_list = D('Recharge')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
    	$page = new \Common\Util\Page(D('Recharge')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
    	//使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('充值申请列表') //设置页面标题
        		->addTopButton('delete')  //添加删除按钮
        		->setSearch('请输入用户ID/用户名/邮箱/手机号', U('index', array('group' => $group)),$recharge_status_list)
        		->SetTabNav($tab_list, $group) //设置Tab按钮列表
        		
                ->addTableColumn('id', 'ID')
                ->addTableColumn('userid', '用户','function','get_username')
                ->addTableColumn('money', '充值金额')
                ->addTableColumn('pay_type','支付方式','function','get_recharge_pay_type_text')
                ->addTableColumn('status', '状态', 'function','get_recharge_status_text')
                ->addTableColumn('ctime', '申请时间', 'time')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->setTableDataPage($page->show()) //数据列表分页
                ->addRightButton('caseButton','',$attr)//批准
                ->addRightButton('self',$attr1)//拒绝
                ->addRightButton('delete') //添加删除按钮
                ->addRightButton('self',$attr_bz)//备注
                ->setExtraHtml($extra_html)
                ->display();
    }
    
    /**
     * 充值审核
     */
    public function review(){
    	$review_type=I('review_type');
    	$recharge_id=I('id');
    
    	$recharge_info=M('Recharge')->where(array('id'=>$recharge_id,'status'=>1))->find();
    	if(empty($recharge_info)){
    		$this->error('该信息不存在!');
    	}
    	if($recharge_info['pay_type']!=1){
    		$this->error('该信息不是线下支付!');
    	}
    	if($review_type==1){//批准
    		M('Recharge')->where(array('id'=>$recharge_id))->setField(array('status'=>2,'utime'=>time()));
    		
    		//充值币种
    		$money_type=$recharge_info['money_type'];
    		//充值金额
    		$money=$recharge_info['money'];
    		if(!($money>0)){
    			$this->error('充值金额不正确！');	
    		}
    		//充值人id
    		$userid=$recharge_info['userid'];
    		//备注
    		$remark="";
    			
    		$moneychange_model=D('Detailed'.ucfirst($money_type));
    		$changedate['type']=3;//流水类型
    		$changedate['money']=$money;//操作金额
    		$changedate['userid']=$userid;//对应用户
    		$changedate['relevant_userid']=1;//起因用户
    		$changedate['remark']=$remark;//详细信息
    		$res=$moneychange_model->changemoney($changedate);//财务流水记录
    		
    		if($res){
    			$log_data['title']=$message_data['title']="批准充值申请";
    			$log_data['to_uid']=$message_data['to_uid']=$userid;
    			$log_data['content']="批准".get_username($userid)."的充值申请，申请ID:".$recharge_info['id'];
    			$message_data['content']="批准了".get_username($userid)."的充值申请，快去查看吧！";
    			add_log_message($log_data,$message_data);
    			$this->success('操作成功!');
    		}else{
    			$this->error('操作失败!');
    		}
    		
    	}elseif($review_type==-1){//拒绝
    		
    		$remark_admin=I('remark');
    	    $userid=$recharge_info['userid'];
    		$log_data['title']=$message_data['title']="拒绝充值申请";
    		$log_data['to_uid']=$message_data['to_uid']=$userid;
    		$log_data['content']="拒绝".get_username($userid)."的充值申请，申请ID:".$recharge_info['id'];
    		$message_data['content']="拒绝了".get_username($userid)."的充值申请，快去查看吧！";
    		add_log_message($log_data,$message_data);
    		M('Recharge')->where(array('id'=>$recharge_id))->setField(array('status'=>-1,'remark_admin'=>$remark_admin,'utime'=>time()));
//     		$this->success('操作成功!');
			$return['status']=1;
			$return['msg']='操作成功';
			$this->ajaxReturn($return);
    	}
    }
 
    /**
     * 后台充值
     */
    public function moneyadd(){
        $uid=is_login();
        if(!$uid){ //还没登录跳转到登录页面
            $this->redirect('Admin/Public/login');
        }
        if(IS_POST){
            //充值用户
            $username=I('username');
            //充值金额
            $money = I('money') ;
            //备注
            $remark=I('remark','充值');
            	
            $userid=M('User')->where(array('username'=>$username))->getField('id');
            if(empty($userid)){
                $this->error('用户不存在！');
            }
            if(!is_numeric($money)){
                $this->error('金额不正确！');
            }
            if(!($money>0)){
                $this->error('金额不正确！');
            }
            
           $moneychange_model=D('DetailedBonus');
			
			$changedate['type']=5;//流水类型
			$changedate['money']=$money;//操作金额
			$changedate['userid']=$userid;//对应用户
			$changedate['relevant_userid']=$uid;//起因用户
			$changedate['remark']=$remark;//详细信息
			$res=$moneychange_model->changemoney($changedate);//财务流水记录
            if($res){
                
                $log_data['title']=$message_data['title']="后台充值";
                $log_data['to_uid']=$message_data['to_uid']=$userid;
                $log_data['content']=$message_data['content']=get_username($uid)."为".$username."充值".$money;
                add_log_message($log_data,$message_data);
                $this->success('操作成功!',U('BonusChange/index'));
            }else{
                $this->error('操作失败!');
            }
            	
        }else{
            $id=I('id');
            if(!empty($id)){
                $uname=get_username($id);
            }
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('后台充值') //设置页面标题
            ->setPostUrl(U('moneyadd')) //设置表单提交地址
            ->addFormItem('username', 'text', '用户名', '用户')
            ->addFormItem('money', 'num', '金额', '金额')
            //->addFormItem('password', 'password', '管理员密码')
            ->addFormItem('remark', 'text', '备注', '备注')
            ->setFormData(array('username'=>$uname))
            ->display();
        }
    }
    /**
     * 后台扣除
     */
    public function moneyred(){
        //币种列表
        $money_type_list=M('MoneyType')->where(array('status'=>1))->select();
        $money_type_arr=list_to_arr($money_type_list, 'identification', 'name');
        if(IS_POST){
            //币种类型
            $money_type=I('money_type');
            //用户
            $userid=I('userid');
            //金额
            $money = I('money') ;
            //备注
            $remark=I('remark','扣除');
    
            if(empty($money_type)){
                $this->error('请选择币种！');
            }
            if(empty($userid)){
                $this->error('请选择用户！');
            }
            if(!is_numeric($money)){
                $this->error('金额不正确！');
            }
            if(!($money>0)){
                $this->error('金额不正确！');
            }
            	
            //当前用户余额
            $user_money=M('User')->where(array('id'=>$userid))->getField('balance_'.$money_type);
            if($user_money<$money){
                $this->error('用户余额不足！');
            }
            $moneychange_model=D('Detailed'.ucfirst($money_type));
    
            $changedate['type']=6;//流水类型
            $changedate['money']=0-$money;//操作金额
            $changedate['userid']=$userid;//对应用户
            $changedate['relevant_userid']=1;//起因用户
            $changedate['remark']=$remark;//详细信息
            $res=$moneychange_model->changemoney($changedate);//财务流水记录
            if($res){
                $log_data['title']=$message_data['title']=$money_type_arr[$money_type]."扣除";
                $log_data['to_uid']=$message_data['to_uid']=$userid;
                $log_data['content']=$message_data['content']="扣除".get_username($userid).$money.$money_type_arr[$money_type];
                add_log_message($log_data,$message_data);
                $this->success('操作成功!',U('admin/BonusChange/index'));
            }else{
                $this->error('操作失败!');
            }
    
        }else{
            $id=I('id');
            if(!empty($id)){
                $map['id']=$id;
            }
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('后台扣除') //设置页面标题
            ->setPostUrl(U('moneyred')) //设置表单提交地址
            ->addFormItem('money_type', 'radio', '币种类型', '币种类型',$money_type_arr)
            ->addFormItem('userid', 'user_list', '用户', '用户',$map)
            ->addFormItem('money', 'num', '金额', '金额')
            ->addFormItem('remark', 'text', '备注', '备注')
            ->display();
        }
    }
    
}
